<?php
class MatchController extends AppController {
    // var $helpers = array ('Html','Form');

    var $name = 'Match'; // riferimento alla cartella delle view
        
    public function beforeFilter(){
    	
		parent::beforeFilter();
        $this->Auth->allow('index','historical','live','livescore','search','searchweek','league','season','team','odds','odds_country','odds_match');
		
    }
    
    public function odds() {
		$this->set("title_for_layout",  __("Quote calcio"));
		$this->set("meta_description",  __("Consultazione delle quote dei principali bookmaker e statistiche delle partite"));
		
	    $this->set("breadcrumb",__("Odds"));
		$this->render("/Match/odds_select_country");
	}
	
    public function odds_country($country) {

		$this->set("title_for_layout",  __("Quote calcio %s", $country));
		$this->set("meta_description",  __("Consultazione delle quote dei principali bookmaker e statistiche delle partite"));
		
	    $this->set("breadcrumb",__("Odds %", $country));
    	$this->set("country",$country);
		$this->render("/Match/odds_country");
    }

    public function odds_match($country, $match) {

		$this->set("title_for_layout",  __("Quote calcio %s", $country));
		$this->set("meta_description",  __("Consultazione delle quote dei principali bookmaker e statistiche delle partite"));
		
	    $this->set("breadcrumb",__("Odds %", $country));
    	$this->set("country",$country);
    	$this->set("matchid",$match);
		$this->render("/Match/odds");
    }

    public function livescore($country=null) {

		if ($country==null) {
			$this->set("title_for_layout",  __("Livescore calcio"));
			$this->set("meta_description",  __("Livescore, aggiornamenti in tempo reale sul risultato delle partite in corso e consultazione calendario delle prossime partite"));
		} else {
			$this->set("title_for_layout",  __("Livescore calcio %s",$country));
			$this->set("meta_description",  __("Risultati calcio in tempo reale e calendario delle prossime partite, nazione: %s",$country));
		}
		
	    $this->set("breadcrumb",__("Live Score"));
    	if (isset($country)) {
	    	$this->set("country",$country);
    	} else {
			$this->render("/Match/livescore_select_country");
    	}
    }

    public function live($country=null) {

		if ($country==null) {
			$this->set("title_for_layout",  __("Live calcio"));
			$this->set("meta_description",  __("Calcio in diretta di tutte le nazioni"));
		} else {
			$this->set("title_for_layout",  __("Live calcio %s",$country));
			$this->set("meta_description",  __("Calcio in diretta, nazione: %s",$country));
		}
    	
		
	    $this->set("breadcrumb",__("Live"));
    	if (isset($country)) {
	    	$this->set("country",$country);
    	} else {
			$this->render("/Match/live_select_country");
    	}
    }


    // public function historical($country=null) {
		// $this->loadModel("Tournament");
// 
		// $tournaments = $this->Tournament->find("all",array("recursive" => 2, "conditions"=>array("Tournament.id"=>$tournamentId)
												// ,"order" => array("Tournament.country", "Tournament.league", "Tournament.season", "Tournament.week")));
		// // $this->set("matches",$this->organize($matches));
		// $this->set("tournaments",$tournaments);
// 		
		// // pr($tournaments);die;
// 
	    // $this->set("breadcrumb",__("Storico calcio"));
//         		
// 
    // }
	
    public function index() {
		$this->set("title_for_layout",  __("Archivio storico calcio"));
		$this->set("meta_description",  __("Archivio storico calcio delle squadre di tutte le nazioni"));
	    $this->set("breadcrumb",__("Storico calcio"));
    }
	
    public function league($country) {
		$this->loadModel("Tournament");

		$leagues = $this->Tournament->find("all", array("recursive" => -1, 'conditions'=>array('Tournament.country_slug'=>$country), 'fields'=>array('DISTINCT Tournament.league','Tournament.league_slug','Tournament.country','Tournament.country_slug'), "order" => array("Tournament.league")));
		// $this->set("matches",$this->organize($matches));
		$this->set("leagues",$leagues);
		
		$this->set("title_for_layout",  __("Archivio storico calcio %s", $country));
		$this->set("meta_description",  __("Archivio storico calcio, nazione %s", $country));
		
	    $this->set("breadcrumb",__("Storico calcio"));
        		

    }
	
    public function season($country,$league) {
		$this->loadModel("Tournament");

		$seasons = $this->Tournament->find("all", array("recursive" => -1, 'conditions'=>array('Tournament.country_slug'=>$country,'Tournament.league_slug'=>$league), 'fields'=>array('DISTINCT Tournament.league','Tournament.league_slug','Tournament.country','Tournament.country_slug','Tournament.season_slug','Tournament.season'), "order" => array("Tournament.season")));
		// $this->set("matches",$this->organize($matches));
		$this->set("seasons",$seasons);
		
		$this->set("title_for_layout",  __("Archivio storico calcio %s %s", $country, $league));
		$this->set("meta_description",  __("Archivio storico calcio, nazione %s, campionato %s", $country, $league));

	    $this->set("breadcrumb",__("Storico calcio"));
        		

    }
	
    public function team($country,$league,$season) {
		$this->loadModel("Tournament");
		$sql = 'select distinct Team.*, t.country, t.country_slug, t.league, t.league_slug, t.season, t.season_slug from tournament t inner join matchs m on t.id=m.tournament_id and t.country_slug='.StringUtil::stringToSql($country).' and t.league_slug='.StringUtil::stringToSql($league).' and t.season_slug='.StringUtil::stringToSql($season)
				.' inner join team Team on (m.localteam_id=Team.id or m.visitorteam_id=Team.id) order by Team.name';
		$teams = $this->Tournament->query($sql);
		$this->set("teams",$teams);
		
		$this->set("title_for_layout",  __("Archivio storico calcio %s %s %s", $country, $league, $season));
		$this->set("meta_description",  __("Archivio storico calcio, nazione %s, campionato %s, stagione %s", $country, $league, $season));

	    $this->set("breadcrumb",__("Storico calcio"));
        		

    }
	
    public function search($country,$league,$season,$team) {
		$this->loadModel("Tournament");
		$this->loadModel("Team");
        $this->Tournament->hasMany["Match"]["conditions"]=array("OR"=>array("Match.localteam_slug"=>$team,"Match.visitorteam_slug"=>$team));
		$tournaments = $this->Tournament->find("all",array("recursive" => 2, "conditions"=>array("Tournament.country_slug"=>$country,"Tournament.league_slug"=>$league,"Tournament.season_slug"=>$season)
								,"order" => array("Tournament.country", "Tournament.league", "Tournament.season", "Tournament.week")));
		// $this->set("matches",$this->organize($matches));
		// pr($tournaments);die;
		$this->set("tournaments",$tournaments);
		$this->set("team",$this->Team->find("first", array("recursive" => -1, "conditions"=>array("Team.name_slug"=>$team,"Team.country_slug"=>$country))));

		$this->set("title_for_layout",  __("Archivio storico calcio %s %s %s %s", $country, $league, $season, $team));
		$this->set("meta_description",  __("Archivio storico calcio, nazione %s, campionato %s, stagione %s, squadra %s", $country, $league, $season, $team));
				
	    $this->set("breadcrumb",__("Storico calcio"));
		$this->render("/Match/historical");

    }
	
    public function searchweek($country, $league, $season, $week) {
		$this->loadModel("Tournament");
		$tournaments = $this->Tournament->find("all",array("recursive" => 2, "conditions"=>array("Tournament.country_slug"=>$country,"Tournament.league_slug"=>$league,"Tournament.season_slug"=>$season,"Tournament.week"=>$week)
								,"order" => array("Tournament.country", "Tournament.league", "Tournament.season", "Tournament.week")));
		// $this->set("matches",$this->organize($matches));
		// pr($tournaments);die;
		$this->set("tournaments",$tournaments);

		$this->set("title_for_layout",  __("Archivio storico calcio %s %s %s %s", $country, $league, $season, $week));
		$this->set("meta_description",  __("Archivio storico calcio, nazione %s, campionato %s, stagione %s, settimana %s", $country, $league, $season, $week));
		
	    $this->set("breadcrumb",__("Storico calcio"));
		$this->render("/Match/historical");

    }
	
	private function organize($matches) {
		$ret = array();
		foreach ($matches as $match) {
			$session = $match["Match"]["country"].$match["Match"]["league"].$match["Match"]["season"].$match["Match"]["week"];
			if (isset($ret[$session])) {
				array_push($ret[$session],$match);
			} else {
				$ret[$session]=array($match);
			}
		}
		return $ret;
	}

}
?>